package com.rongke.web.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.User;
import com.rongke.model.UserIdentity;
import com.rongke.service.UserIdentityService;
import com.rongke.service.UserService;
import com.rongke.web.apix.TongdunYYSAuth;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/api/TDGJJ")
@Transactional
@CrossOrigin
public class TDGJJController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private UserService userService;
    @Autowired
    private UserIdentityService userIdentityService;

    /**
     * 同盾公积金获取地区
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/getAttr", method = RequestMethod.GET)
    public JsonResp getAttr() throws Exception {
        JSONObject result = TongdunYYSAuth.getAttr();
        return JsonResp.ok(result);
    }


    /**
     * 查询登录要素
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/gjjGetLoginFactor", method = RequestMethod.GET)
    public JsonResp gjjGetLoginFactor(String channelCode) throws Exception {
        JSONObject result = TongdunYYSAuth.gjjGetLoginFactor(channelCode);
        return JsonResp.ok(result);
    }

    /**
     * 创建任务
     * @param channelCode
     * @param phone
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/gjjCreateTask", method = RequestMethod.GET)
    public JsonResp gjjCreateTask(String channelCode) throws Exception {
        User user = userService.findLoginUser();
        EntityWrapper<UserIdentity> ew = new EntityWrapper<>();
        ew.eq("user_id",user.getId());
        UserIdentity userIdentity = userIdentityService.selectOne(ew);
        JSONObject createTaskString = TongdunYYSAuth.gjjCreateTask(channelCode,user.getUserName(),userIdentity.getIdentityNum(),user.getPhone(),String.valueOf(user.getId()));
        return JsonResp.ok(createTaskString);
    }

    /**
     * 登录验证接口
     * @param taskStage
     * @param smsCode
     * @param taskId
     * @param userName
     * @param userPass
     * @param authCode
     * @param loginType
     * @param requestType
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/gjjLoginCheck", method = RequestMethod.GET)
    public JsonResp gjjLoginCheck(String taskStage, String smsCode, String taskId, String userName, String userPass, String authCode, String loginType, String requestType) throws Exception {
        JSONObject result = TongdunYYSAuth.gjjLoginCheck(taskStage,smsCode,taskId,userName,userPass,authCode,loginType,requestType);
        return JsonResp.ok(result);
    }






}
